import numpy as np
class Query:
    def __init__(self, origin, origintype):
        self.origin = origin
        self.origintype = sorted(origintype)

    def query(self, ta, case_type):
        self.ta = ta
        self.case_type = case_type
        if self.case_type not in self.origin:
            self.case_type = list(self.origin.keys())[0]

        return self._handle()

    def _handle(self):
        clf = self.origin[self.case_type][0]
        dic = self.origin[self.case_type][1]
        x = np.random.randint(1, size=len(dic))
        for t in self.ta:
            for key, val in t.items():
                if len(val) == 0:
                    continue
                for v in val:
                    tk = "%s_%s" % (key, v)
                    if tk not in dic:
                        continue
                    tv = dic[tk]
                    x[tv] = 1
        p = clf.predict([x])[0]
        xs = clf.predict_proba([x])
        return p, xs[0][self.origintype.index(p)], list(zip(self.origintype,xs[0]))